/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MencatatTransaksi1.java
 *
 * Created on Nov 10, 2011, 4:51:03 PM
 */
package Kasir;

import Entity.Barang;
import Manager.TestConnection;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Piyo
 */
public class MencatatTransaksi extends javax.swing.JPanel {

    private int maxJumlah = -1;

    /** Creates new form MencatatTransaksi1 */
    public MencatatTransaksi() {
        initComponents();
        kodeField.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dateLabel = new javax.swing.JLabel();
        noNotaLabel = new javax.swing.JLabel();
        noNotaField = new javax.swing.JTextField();
        kembaliField = new javax.swing.JTextField();
        kodeLabel = new javax.swing.JLabel();
        namaBarangLabel = new javax.swing.JLabel();
        namaBarangField = new javax.swing.JTextField();
        hargaBarangLabel = new javax.swing.JLabel();
        hargaBarangField = new javax.swing.JTextField();
        jumlahLabel = new javax.swing.JLabel();
        jumlahField = new javax.swing.JTextField();
        bayarField = new javax.swing.JTextField();
        DetailTransaksiLabel = new javax.swing.JLabel();
        bayarLabel = new javax.swing.JLabel();
        kodeField = new javax.swing.JTextField();
        tanggalChooser = new com.toedter.calendar.JDateChooser();
        hapusButton = new javax.swing.JButton();
        printButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        kembaliLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        detailTransaksiTable = new javax.swing.JTable();
        totalFakturField = new javax.swing.JTextField();
        totalFakturLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        batalButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        dateLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 12));
        dateLabel.setText("Date");

        noNotaLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 12));
        noNotaLabel.setText("No Nota");

        noNotaField.setEditable(false);
        noNotaField.setText(transaksiController.getIDTransaksi());

        kembaliField.setEditable(false);

        kodeLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        kodeLabel.setText("Kode");

        namaBarangLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        namaBarangLabel.setText("Nama Barang");

        namaBarangField.setEditable(false);

        hargaBarangLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        hargaBarangLabel.setText("Harga Barang");

        hargaBarangField.setEditable(false);

        jumlahLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        jumlahLabel.setText("Jumlah");

        jumlahField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jumlahFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jumlahFieldKeyReleased(evt);
            }
        });

        bayarField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bayarFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                bayarFieldKeyReleased(evt);
            }
        });

        DetailTransaksiLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        DetailTransaksiLabel.setText("Detail Transaksi ");

        bayarLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        bayarLabel.setText("Bayar");

        kodeField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                kodeFieldKeyPressed(evt);
            }
        });

        tanggalChooser.setDate(new GregorianCalendar().getTime());
        tanggalChooser.setDateFormatString("dd-MM-yyyy");
        tanggalChooser.setEnabled(false);

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });

        printButton.setText("Print");
        printButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        kembaliLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        kembaliLabel.setText("Kembali");

        detailTransaksiTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "no.", "Kode Barang", "Nama Barang", "Jumlah", "Harga", "Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        detailTransaksiTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                detailTransaksiTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(detailTransaksiTable);
        detailTransaksiTable.getColumnModel().getColumn(0).setMinWidth(30);
        detailTransaksiTable.getColumnModel().getColumn(0).setMaxWidth(30);

        totalFakturField.setEditable(false);
        totalFakturField.setText("0.0");

        totalFakturLabel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 11));
        totalFakturLabel.setText("Total Pembayaran");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36));
        jLabel1.setText("Transaksi Agatha");

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(kodeLabel)
                                        .addComponent(kodeField, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(25, 25, 25)
                                            .addComponent(cancelButton)
                                            .addGap(39, 39, 39)
                                            .addComponent(hapusButton)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(namaBarangLabel)
                                                .addComponent(namaBarangField, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(82, 82, 82)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(totalFakturLabel)
                                        .addComponent(bayarLabel)
                                        .addComponent(kembaliLabel))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(totalFakturField, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(kembaliField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                                        .addComponent(bayarField, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                                        .addComponent(batalButton))
                                    .addGap(58, 58, 58))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(47, 47, 47)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jumlahLabel)
                                        .addComponent(jumlahField, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(51, 51, 51)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(hargaBarangField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(dateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(noNotaLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGap(30, 30, 30)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(tanggalChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(noNotaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addComponent(hargaBarangLabel))
                                    .addGap(54, 54, 54)))
                            .addGap(18, 18, 18))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(DetailTransaksiLabel)
                            .addContainerGap(743, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 715, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(printButton)
                        .addGap(152, 152, 152))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dateLabel)
                            .addComponent(tanggalChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(noNotaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(noNotaLabel)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hargaBarangLabel)
                    .addComponent(jumlahLabel)
                    .addComponent(kodeLabel)
                    .addComponent(namaBarangLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hargaBarangField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jumlahField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(namaBarangField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kodeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DetailTransaksiLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(totalFakturLabel)
                    .addComponent(totalFakturField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cancelButton)
                            .addComponent(hapusButton))
                        .addGap(2, 2, 2))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bayarLabel)
                        .addComponent(bayarField, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kembaliField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kembaliLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(printButton)
                    .addComponent(batalButton))
                .addContainerGap(14, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    kodeField.setText(null);
    kodeField.setEditable(true);
    namaBarangField.setText(null);
    jumlahField.setText(null);
    hargaBarangField.setText(null);
}//GEN-LAST:event_cancelButtonActionPerformed

private void kodeFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        try {
            Barang barang = transaksiController.getDataBarang(kodeField.getText());
            if (barang != null) {
                namaBarangField.setText(barang.getNamabarang());
                hargaBarangField.setText(barang.getHargabarang() + "");
                maxJumlah = barang.getStock();
                jumlahField.requestFocus();
                kodeField.setEditable(false);
            } else {
                JOptionPane.showMessageDialog(null, "Kode barang salah atau tidak ada");
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }
}//GEN-LAST:event_kodeFieldKeyPressed

private void bayarFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bayarFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        double total = Double.parseDouble(totalFakturField.getText());
        double bayar = Double.parseDouble(bayarField.getText());
        if (bayar < total) {
            JOptionPane.showMessageDialog(null, "Uang pembayaran kurang");
        } else {
            double kembali = bayar - total;
            kembaliField.setText(String.valueOf(kembali));
        }
    }
}//GEN-LAST:event_bayarFieldKeyPressed

private void jumlahFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jumlahFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        if (kodeField.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "isi field kode barang");
            kodeField.transferFocus();
            return;
        }
        if (jumlahField.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "isi field jumlah barang");
            kodeField.transferFocus();
            return;
        }
        int index = -1;
        for (int i = 0; i < detailTransaksiTable.getRowCount(); i++) {
            if (detailTransaksiTable.getValueAt(i, 1).equals(kodeField.getText())) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            double hargaBarang = Double.parseDouble(hargaBarangField.getText());
            double jumlahBarang = Double.parseDouble(jumlahField.getText());
            if (jumlahBarang > maxJumlah) {
                JOptionPane.showMessageDialog(null, "jumlah lebih dari stok yaitu " + maxJumlah);
                return;
            }
            maxJumlah = -1;
            double total = hargaBarang * jumlahBarang;
            Object[] ob = {index + 1,
                kodeField.getText(),
                namaBarangField.getText(),
                jumlahField.getText(),
                hargaBarangField.getText(),
                total
            };
            Double tot_bef = Double.parseDouble(((DefaultTableModel) detailTransaksiTable.getModel()).getValueAt(index, 5).toString());
            ((DefaultTableModel) detailTransaksiTable.getModel()).removeRow(index);
            ((DefaultTableModel) detailTransaksiTable.getModel()).insertRow(index, ob);
            tot_bef = Double.parseDouble(totalFakturField.getText()) - tot_bef + total;
            totalFakturField.setText(tot_bef + "");
        } else {
            double hargaBarang = Double.parseDouble(hargaBarangField.getText());
            double jumlahBarang = Double.parseDouble(jumlahField.getText());
            double total = hargaBarang * jumlahBarang;
            if (jumlahBarang > maxJumlah) {
                JOptionPane.showMessageDialog(null, "jumlah lebih dari stok yaitu " + maxJumlah);
                return;
            }
            DefaultTableModel model = (DefaultTableModel) detailTransaksiTable.getModel();

            Object[] ob = {detailTransaksiTable.getRowCount() + 1,
                kodeField.getText(),
                namaBarangField.getText(),
                jumlahField.getText(),
                hargaBarangField.getText(),
                total
            };
            model.insertRow(detailTransaksiTable.getRowCount(), ob);

            double sum = Double.parseDouble(totalFakturField.getText()) + total;
            totalFakturField.setText("" + sum);
        }

        kodeField.setText(null);
        jumlahField.setText(null);
        namaBarangField.setText(null);
        hargaBarangField.setText(null);
        kodeField.setEditable(true);
        kodeField.requestFocus();
    }
}//GEN-LAST:event_jumlahFieldKeyPressed

private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
    int row = detailTransaksiTable.getSelectedRow();
    Double x = (Double) detailTransaksiTable.getValueAt(row, 5);
    Double t = Double.parseDouble(totalFakturField.getText());
    Double s = t - x;
    totalFakturField.setText(String.valueOf(s));
    ((DefaultTableModel) detailTransaksiTable.getModel()).removeRow(row);
    for (int i = 0; i < detailTransaksiTable.getRowCount(); i++) {
        detailTransaksiTable.setValueAt(i + 1, i, 0);
    }

    kodeField.setText(null);
    jumlahField.setText(null);
    namaBarangField.setText(null);
    hargaBarangField.setText(null);
}//GEN-LAST:event_hapusButtonActionPerformed

private void detailTransaksiTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_detailTransaksiTableMouseClicked
    try {
        int row = detailTransaksiTable.getSelectedRow();
        String kode = detailTransaksiTable.getValueAt(row, 1).toString();
        String namabarang = detailTransaksiTable.getValueAt(row, 2).toString();
        String jumlah = detailTransaksiTable.getValueAt(row, 3).toString();
        String harga = detailTransaksiTable.getValueAt(row, 4).toString();

        kodeField.setText(kode);
        kodeField.setEditable(false);
        namaBarangField.setText(namabarang);
        jumlahField.setText(jumlah);
        hargaBarangField.setText(harga);
        maxJumlah = Barang.getStokR(kode);
    } catch (SQLException ex) {
        Logger.getLogger(MencatatTransaksi.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_detailTransaksiTableMouseClicked

private void printButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printButtonActionPerformed
    if (!bayarField.getText().equals("") && !kembaliField.getText().equals("")) {
        try {
            double total = Double.parseDouble(totalFakturField.getText());
            double bayar = Double.parseDouble(bayarField.getText());
            if (bayar < total) {
                JOptionPane.showMessageDialog(null, "Uang pembayaran kurang");
                return;
            } else {
                double kembali = bayar - total;
                kembaliField.setText(String.valueOf(kembali));
            }
            DefaultTableModel ds = (DefaultTableModel) detailTransaksiTable.getModel();
            JRTableModelDataSource datas = new JRTableModelDataSource(ds);
            String reportSource = "./reports/NOTA.jrxml";

            JasperReport jr = JasperCompileManager.compileReport(reportSource);


            Map<String, Object> params = new HashMap<String, Object>();
            params.put("totalpembayaran", this.totalFakturField.getText());
            params.put("bayar", this.bayarField.getText());
            params.put("kembalian", this.kembaliField.getText());
            JasperPrint jp = JasperFillManager.fillReport(jr, params, datas);

            JasperViewer.viewReport(jp, false);
            Connection conn = new TestConnection().getConnection();
            String sql = "insert into TRANSAKSI values (?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, noNotaField.getText());
            java.sql.Date date = new java.sql.Date(tanggalChooser.getDate().getYear(), tanggalChooser.getDate().getMonth(), tanggalChooser.getDate().getDate());
            ps.setDate(2, date);
            ps.setDouble(3, Double.parseDouble(totalFakturField.getText()));
            ps.executeUpdate();
            ps.close();

            for (int i = 0; i < detailTransaksiTable.getRowCount(); i++) {
                sql = "insert into DETAILTRANSAKSI values (?,?,?,?)";
                ps = conn.prepareStatement(sql);
                ps.setString(1, (String) detailTransaksiTable.getValueAt(i, 1));
                ps.setString(2, noNotaField.getText());
                ps.setInt(3, Integer.parseInt(detailTransaksiTable.getValueAt(i, 3).toString()));
                ps.setDouble(4, ((Double) detailTransaksiTable.getValueAt(i, 5)).doubleValue());
                ps.executeUpdate();
                ps.close();
            }

            for (int i = 0; i < detailTransaksiTable.getRowCount(); i++) {
                String kode = (String) detailTransaksiTable.getValueAt(i, 1);
                sql = "select STOK from BARANG where KODEBARANG = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, kode);
                ResultSet rs = ps.executeQuery();
                rs.next();
                int stokawal = rs.getInt(1);
                ps.close();

                int stok = stokawal - Integer.parseInt(detailTransaksiTable.getValueAt(i, 3).toString());
                sql = "update BARANG set STOK = ? where KODEBARANG = ?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1, stok);
                ps.setString(2, kode);
                ps.executeUpdate();
                ps.close();
            }

            conn.close();

            while (detailTransaksiTable.getRowCount() > 0) {
                ((DefaultTableModel) detailTransaksiTable.getModel()).removeRow(0);
            }


            noNotaField.setText(transaksiController.getIDTransaksi());
            totalFakturField.setText("0.0");
            tanggalChooser.setDate(new GregorianCalendar().getTime());
            bayarField.setText("");
            kembaliField.setText("");


        } catch (JRException ex) {
            Logger.getLogger(MencatatTransaksi.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(MencatatTransaksi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_printButtonActionPerformed

private void jumlahFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jumlahFieldKeyReleased
    if (!jumlahField.getText().matches("[0-9]*")) {
        JOptionPane.showMessageDialog(null, "Masukkan harus berupa angka");
        this.jumlahField.setText(jumlahField.getText().substring(0, jumlahField.getText().length() - 1));
    }
}//GEN-LAST:event_jumlahFieldKeyReleased

private void bayarFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bayarFieldKeyReleased
    if (!bayarField.getText().matches("[0-9]*")) {
        JOptionPane.showMessageDialog(null, "Masukkan harus berupa angka");
        this.bayarField.setText(bayarField.getText().substring(0, bayarField.getText().length() - 1));
    }
}//GEN-LAST:event_bayarFieldKeyReleased

private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
    while (detailTransaksiTable.getRowCount() > 0) {
        ((DefaultTableModel) detailTransaksiTable.getModel()).removeRow(0);
    }

    noNotaField.setText(transaksiController.getIDTransaksi());
    totalFakturField.setText("0.0");
    tanggalChooser.setDate(new GregorianCalendar().getTime());
    bayarField.setText("");
    kembaliField.setText("");
}//GEN-LAST:event_batalButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel DetailTransaksiLabel;
    private javax.swing.JButton batalButton;
    private javax.swing.JTextField bayarField;
    private javax.swing.JLabel bayarLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JTable detailTransaksiTable;
    private javax.swing.JButton hapusButton;
    private javax.swing.JTextField hargaBarangField;
    private javax.swing.JLabel hargaBarangLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jumlahField;
    private javax.swing.JLabel jumlahLabel;
    private javax.swing.JTextField kembaliField;
    private javax.swing.JLabel kembaliLabel;
    private javax.swing.JTextField kodeField;
    private javax.swing.JLabel kodeLabel;
    private javax.swing.JTextField namaBarangField;
    private javax.swing.JLabel namaBarangLabel;
    private javax.swing.JTextField noNotaField;
    private javax.swing.JLabel noNotaLabel;
    private javax.swing.JButton printButton;
    private com.toedter.calendar.JDateChooser tanggalChooser;
    private javax.swing.JTextField totalFakturField;
    private javax.swing.JLabel totalFakturLabel;
    // End of variables declaration//GEN-END:variables
}
